package com.aierjun.testservice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private TextView tv;
    private EditText AddWeb;
    private String URL="http://www.kuaidi100.com/query?type=zhongtong&postid=405123024058";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn= (Button) findViewById(R.id.btn);
        tv= (TextView) findViewById(R.id.test);
        AddWeb= (EditText) findViewById(R.id.add);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new NetworkGetThrea().start();
                //new NetworkPostThrea().start();
                btn.setText("请求中...");
                btn.setEnabled(false);
            }
        });
    }

    class NetworkGetThrea extends Thread{
        public void run(){
            HttpClient httpClient=new DefaultHttpClient();
            HttpGet httpGet=new HttpGet(URL);
            try {
                HttpResponse httpResponse=httpClient.execute(httpGet);
                if (httpResponse.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                    HttpEntity httpEntity=httpResponse.getEntity();
                    final String entityStr= EntityUtils.toString(httpEntity,"UTF-8");
                    Log.d("aierjunLog",entityStr);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btn.setEnabled(true);
                            btn.setText("请求");
                            tv.setText(entityStr);
                            tv.append("\n解析中...\n");
                            tv.append(pareJSONStr(entityStr));
                        }
                    });
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    private  String pareJSONStr(String jsonStr){
        try {
            JSONObject jsonObject=new JSONObject(jsonStr);
            int status=jsonObject.getInt("status");
            String message=jsonObject.getString("message");
            return "状态码为"+status+"\n消息体为"+message;
        }catch (JSONException e){
            e.printStackTrace();
        }
        return "解析错误";
    }


    class NetworkPostThrea extends Thread{
        public void run(){
            HttpClient httpClient=new DefaultHttpClient();

            List<BasicNameValuePair> params=new ArrayList<>();
            BasicNameValuePair wordParam=new BasicNameValuePair("wprd","Android");
            BasicNameValuePair pageParam=new BasicNameValuePair("page","1");
            params.add(wordParam);
            params.add(pageParam);
            String paramStr= URLEncodedUtils.format(params,"UTF-8");
            //下面两句相等（等价）
            HttpGet httpGet=new HttpGet("http://www.baidu.com/baidu"+"?"+paramStr);
            HttpGet httpGet1=new HttpGet("http://www.baidu.com/baidu?word=Android&page=1");
            try {
                HttpResponse httpResponse=httpClient.execute(httpGet);
                if (httpResponse.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                    HttpEntity httpEntity=httpResponse.getEntity();
                    final String entityStr= EntityUtils.toString(httpEntity);
                    Log.d("aierjunLog",entityStr);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btn.setEnabled(true);
                            btn.setText("请求");
                            tv.setText(entityStr);

                        }
                    });
                }
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
